<?php

namespace backend\controllers;

use Yii;
use backend\models\GoodsClass;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\Logs;
/**
 * GoodsClassController implements the CRUD actions for GoodsClass model.
 */
class GoodsClassController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
            'access'=>[
                'class'=> \backend\components\PermissionBehavior::className(),

            ]
        ];
    }

    /**
     * Lists all GoodsClass models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => GoodsClass::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single GoodsClass model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new GoodsClass model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {

        $model = new GoodsClass();
        $model -> sort = 100;
        $model -> createtime = date("Y-m-d H:i:s");
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $logs = new Logs();
            $logs->opid = Yii::$app->user->id;
            $logs->oprtype = '创建分类';
            $logs->remark = $model->classname;
            $logs->oprtime = date("Y-m-d H:i:s");
            $logs->save();
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing GoodsClass model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $remark='';
        if($model->classname != Yii::$app->request->post('GoodsClass')['classname']){
            $remark.='修改分类名称（'.$model->classname .' to '. Yii::$app->request->post('GoodsClass')['classname'] . '） ';
        }
        if($model->sort != Yii::$app->request->post('GoodsClass')['sort']){
            $remark.='修改分类排序（'.$model->sort .' to '. Yii::$app->request->post('GoodsClass')['sort'] . '） ';
        }
        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            $logs = new Logs();
            $logs->opid = Yii::$app->user->id;
            $logs->oprtype = '修改分类';
            $logs->remark = $remark;
            $logs->oprtime = date("Y-m-d H:i:s");
            $logs->save();
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing GoodsClass model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $logs = new Logs();
        $logs->opid = Yii::$app->user->id;
        $logs->oprtype = '删除分类';
        $logs->remark = $model->classname;
        $logs->oprtime = date("Y-m-d H:i:s");
        $logs->save();
        $model->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the GoodsClass model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return GoodsClass the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = GoodsClass::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
